//
//  CustomPushAnimated.m
//  Camera
//
//  Created by gml on 2018/12/18.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "CustomPushAnimatedPush.h"
@interface CustomPushAnimatedPush()
@property (nonatomic, strong)id<UIViewControllerContextTransitioning> transitionContext;
@end

@implementation CustomPushAnimatedPush
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.7;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;
    // 获取源控制器
//    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 获取目标控制器
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 获取容器视图
    UIView *containView = [transitionContext containerView];
    // 将控制器添加到容器中 目标控制器在后
//    [containView addSubview:fromVC.view];
    
    CGRect finalRect = [transitionContext finalFrameForViewController:toVC];
    toVC.view.frame = CGRectOffset(finalRect, [UIScreen mainScreen].bounds.size.width, 0);
    [containView addSubview:toVC.view];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:0.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        toVC.view.frame = finalRect;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
    
    
}
@end
